package dao;


import Utils.BaseDao;
import bean.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao{

    @Override
    public User login(User user) throws SQLException {
        Connection conn = BaseDao.conn();
        String sql = "select * from admin where uname=? and upwd=?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setObject(1,user.getUsername());
            ps.setObject(2,user.getPassword());
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                User user1 = new User(rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3));
                return user1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
//        String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
//        return this.getBean(sql, user.getUsername(), user.getPassword(), user.getEmail());
        Connection conn = BaseDao.conn();
        String sql = "insert into food set username=?,password=?,email=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setObject(1,user.getUsername());
            ps.setObject(2,user.getPassword());
            ps.setObject(3,user.getEmail());
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            BaseDao.close(null,ps,conn);
        }
        return 0;
    }

}
